package com.fantabest.frontend.client.mvc;

import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.LayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.user.client.ui.Widget;

public class BorderedContainer extends LayoutContainer {

	private int borderSize = 10;
	
	private LayoutContainer tableContainer;
	
	private TableData tlData = new TableData();
	private TableData tcData = new TableData();
	private TableData trData = new TableData();
	private TableData mlData = new TableData();
	private TableData mcData = new TableData();
	private TableData mrData = new TableData();
	private TableData blData = new TableData();
	private TableData bcData = new TableData();
	private TableData brData = new TableData();
	
	
	public BorderedContainer() {
		super();
		initContainer();
	}

	public BorderedContainer(Layout layout) {
		super(layout);
		initContainer();
	}
	
	private String getStringSize(int size)	{
		return size + "px";
	}
	
	private void initContainer()	{
		tableContainer = new LayoutContainer();
		TableLayout layout = new TableLayout();
		layout.setColumns(3);
		layout.setHeight("100%");
		layout.setWidth("100%");
		tableContainer.setLayout(layout);
		
		tableContainer.setStyleName("border-container-style");
//		Setto le dimensioni delle celle del bordo
		tlData.setWidth(getStringSize(borderSize));
		tlData.setHeight(getStringSize(borderSize));
		tlData.setStyleName("xxx-tl");
		
		tcData.setHeight(getStringSize(borderSize));
		tcData.setStyleName("xxx-tc");
		
		trData.setWidth(getStringSize(borderSize));
		trData.setHeight(getStringSize(borderSize));
		trData.setStyleName("xxx-tr");
		
		mlData.setWidth(getStringSize(borderSize));
		mlData.setStyleName("xxx-ml");
		
		mrData.setWidth(getStringSize(borderSize));
		mrData.setStyleName("xxx-mr");
		
		blData.setWidth(getStringSize(borderSize));
		blData.setHeight(getStringSize(borderSize));
		blData.setStyleName("xxx-bl");
		
		bcData.setHeight(getStringSize(borderSize));
		bcData.setStyleName("xxx-bc");
		
		brData.setWidth(getStringSize(borderSize));
		brData.setHeight(getStringSize(borderSize));
		brData.setStyleName("xxx-br");
		
		this.add(tableContainer);
	}
	
	private boolean addMainComponent(Widget widget, LayoutData layoutData)	{
		tableContainer.removeAll();
		
		tableContainer.add(createHtml(), tlData);
		tableContainer.add(createHtml(), tcData);
		tableContainer.add(createHtml(), trData);
		
		tableContainer.add(createHtml(),mlData);
		if(layoutData == null)
			tableContainer.add(widget);
		else
			tableContainer.add(widget, layoutData);
		tableContainer.add(createHtml(),mrData);
		
		tableContainer.add(createHtml(), blData);
		tableContainer.add(createHtml(), bcData);
		tableContainer.add(createHtml(), brData);
		
		return true;
	}
	
	@Override
	protected boolean add(Component item) {
		return addMainComponent(item,null);
	}
	
	@Override
	public boolean add(Widget widget) {
		return addMainComponent(widget,null);
	}
	
	@Override
	public boolean add(Widget widget, LayoutData layoutData) {
		return addMainComponent(widget, layoutData);
	}
	
	private Html createHtml()	{
		Html iHtml = new Html("<i> </i>");
		return iHtml;
	}

}
